#!/bin/bash
set -ex

version=`date +%Y-%m-%d -d "1 day ago"`
# 为了保持mongodb的数据每天同步最新数据至客户 现把导出时间写死为
YESTERDAY=`date +%Y-%m-%d -d "2 day ago"`
DATE_MIN=${YESTERDAY}" 22:00:00"
NOW=`date "+%Y-%m-%d %H:%M:%S"`
LAST_TIME_FILE=last_time


DIR_DUMP=${version}
FILE_STATUS=status.txt

DIR_TARGET=/home/nfs/mongo_temp_dump
remote_dir=/home/nfs/server-download-dir/mongodb-dump
CODE_DIR=/home/work/apps/airflow/apps/mongodb_dump

mkdir -p ${DIR_TARGET}
mkdir -p ${remote_dir}
mkdir -p ${DIR_TARGET}/${DIR_DUMP}

# 获得上次导出时间
if [ -f ${CODE_DIR}/${LAST_TIME_FILE} ]; then
    DATE_MIN=`cat ${CODE_DIR}/${LAST_TIME_FILE}`
fi

MONGO_HOME=/home/work/env/mongodb
MONGO_ADDR=172.16.215.45:40042
MONGO_DB=app_data
USERNAME=readme
PASSWORD=readme


cd ${DIR_TARGET}/${DIR_DUMP}

if [ ! -f ${FILE_STATUS} ];then
    touch status.txt
fi

for item in acquirer_event annual_reports baidu_news bid_detail bulletin company_data company_information company_information_pedata court_ktgg enterprise_data_gov enterprise_owing_tax ershoufang_anjuke ershoufang_fangdd ershoufang_lianjia exchange_rate exit_event financing_events financing_events_innotree investment_events investment_funds investment_institutions investment_institutions_innotree judge_process judgement_wenshu land_auction land_auction_fang land_project_selling land_selling_auction listing_events loupan_anjuke loupan_fangdd loupan_lianjia neighborhood_anjuke net_loan_blacklist news nssgs_info patent penalty ppp_project shixin_info ssgs_baseinfo ssgs_caibao_assets_liabilities ssgs_caibao_cash_flow ssgs_caibao_companies_ability ssgs_caibao_profit ssgs_notice_cninfo ssgs_regular_report ssgs_rules tax_payer_level_A top_ten_shareholder xiaoqu_fangdd xiaoqu_lianjia zhixing_info industry_news enterprise_data_gov_change_info exchange_rate
do
   ${MONGO_HOME}/bin/mongoexport -h ${MONGO_ADDR} -d ${MONGO_DB} -c ${item} --query "{\"_utime\":{\$gte: \"${DATE_MIN}\"}}" -u ${USERNAME} -p ${PASSWORD} -o "$item.json"
     zip "$item.zip" "$item.json"
     line_num=`wc -l ${item}.json | awk '{print $1}'`
     echo "$item.zip||${line_num}||${DATE_MIN}||$(date "+%Y-%m-%d %H:%M:%S")" >> status.txt
     rm -r "$item.json"
done


# 先删除已经存在的数据文件
rm -rf ${remote_dir}/${DIR_DUMP}
mv ${DIR_TARGET}/${DIR_DUMP} ${remote_dir}

# 把最后导出记录写入文件
echo ${NOW} > ${CODE_DIR}/${LAST_TIME_FILE}